print0=print
from loguru import logger
import os,datetime,sys
#实现print config既可；
#config(logfile=os.path.join(keywordinfo.logpath, os.path.split(__file__)[-1]), level="info",logfreq="d", isscreen=True,max_logfiles=3)
level_info={"debug":"DEBUG","info":"INFO","warning":"WARNING","sys":"WARNING","error":"ERROR"}
level_info_h={"debug":logger.debug,"info":logger.info,"warning":logger.warning,"sys":logger.warning,"error":logger.error}
level_infokeys=list(level_info.keys())
level_info_n={"debug":5,"info":4,"warning":3,"sys":2,"error":1}
curr_level_n=4
isdetail=True
def config(logfile="",logfreq="",isscreen=True,level="info",max_logfiles=2,enqueue=False,details=True):
    global curr_level_n,isdetail
    isdetail=details
    if max_logfiles:
        d0=os.path.dirname(logfile)
        if os.path.exists(d0):
            fname=os.path.basename(logfile)
            l=os.listdir(d0)
            s=[]
            for i in l:
                if fname in i:
                    s.append(i)
            s.sort()
            n=len(s)-max_logfiles
            if n>0:
                for i in range(n):
                    j=s.pop(0)
                    url=os.path.join(d0,j)
                    os.remove(url)
                    print("remove",url)
    isyes = False
    if level in level_info_n.keys():
        curr_level_n=level_info_n[level]
    if logfile != "":
        logfile = os.path.abspath(logfile)
        root = os.path.dirname(logfile)
        if os.path.exists(root):
            if os.access(root, os.W_OK):
                isyes = True
        else:
            try:
                os.makedirs(root)
                isyes = True
            except Exception as e:
                logger.error(e)
                isyes = False
        if isyes:
            now = datetime.datetime.now()
            if logfile[-4:] == ".log":
                logfile = logfile[:-4]
            if logfreq in ["d", "day"]:
                logfile = logfile + now.strftime("%Y%m%d") + ".log"
            elif logfreq in ["m", "month"]:
                logfile = logfile + now.strftime("%Y%m") + ".log"
            elif logfreq in ["y", "year"]:
                logfile = logfile + now.strftime("%Y") + ".log"
            else:
                logfile = logfile + ".log"
        else:
            logfile = "log.log"
        logger.add(sink=logfile,encoding="utf-8",format="{time} {level} {message}",level=level_info[level],enqueue=enqueue)
    else:
        logger.level(level_info[level])

def print(*args):
    global curr_level_n,isdetail
    f=args[0]
    if f in level_infokeys:
        n=level_info_n[f]
        h = level_info_h[f]
        msg=args[1:]
    else:
        n=4
        h=logger.info
        msg=args

    if n<=curr_level_n:
        if isdetail:
            filename = sys._getframe().f_back.f_code.co_filename
            funcName = sys._getframe().f_back.f_code.co_name  # 获取调用函数名
            lineNumber = sys._getframe().f_back.f_lineno  # 获取行号
            # h((filename,funcName,lineNumber))
            h((filename,funcName,lineNumber,msg))
        else:
            h(msg)
